package com.jkoss.wine.system.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.Constant;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.entity.SystemSetting;
import com.jkoss.wine.system.service.ISystemSettingService;

/**
 * 系统设置 前端控制器
 * 
 * @Author Jason
 * @Version 1.0, 2018-09-25
 * @See
 * @Since com.jkoss.mengal.system.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/system/systemSetting")
public class SystemSettingController extends BaseController {

	@Autowired
	private ISystemSettingService iSystemSettingService;

	/**
	 * 列表
	 * 
	 * @param key
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/system/systemSetting/list")
	public String list(String key, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		dwzPageBean.getCountResultMap().put("key", key);
		Wrapper wrapper = Condition.create().ne("del", 1).like("ky", key);

		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("add_time", false);
		}

		Page resultPage = iSystemSettingService.selectPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "system/systemSetting/list";
	}

	/**
	 * 去新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/system/systemSetting/toInsert")
	public String toInsert(HttpServletRequest request, HttpServletResponse response) {
		return "system/systemSetting/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/system/systemSetting/toInsert")
	@ResponseBody
	public Object insert(@Valid SystemSetting system_setting, HttpServletRequest request, HttpServletResponse response) {

		List<SystemSetting> systemSettings = iSystemSettingService.selectList(Condition.create().eq("ky", system_setting.getKy()).ne("del", 1));

		if (!CommonUtil.isBlank(systemSettings)) {
			return ajaxError("该key已经存在");
		}

		// 获取当前时间
		system_setting.setAddTime(CommonUtil.getDate());
		// 获取当前操作人，当操作人为null时，表示自动录入（方法一）
		MerchantEmployees employees = (MerchantEmployees) request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		system_setting.setOperator(employees.getEmployeeId());
		// 方法二：warehouse.setOperator(getUserId());
		// 是否删除：0-未删除、1-已删除、2-不能删除
		system_setting.setDel(0);
		if (iSystemSettingService.insert(system_setting)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/system/systemSetting/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iSystemSettingService.selectById(id));
		return "system/systemSetting/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/system/systemSetting/toUpdate")
	@ResponseBody
	public Object update(@Valid SystemSetting system_setting, HttpServletRequest request, HttpServletResponse response) {
		if (iSystemSettingService.updateById(system_setting)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/system/systemSetting/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList<>();
		for (String string : id) {
			SystemSetting systemSetting = new SystemSetting();
			systemSetting.setId(string);
			//// 是否删除：0-未删除、1-已删除、2-不能删除
			systemSetting.setDel(1);
			list.add(systemSetting);
		}
		if (iSystemSettingService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

}
